﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace Device.ViewModels
{
    internal class DeviceDialogViewModel : BindableBase,IDialogAware
    {

        private string _ClosingTips;
        /**
         * 调用这个事件来申请关闭弹窗 
         */ 
        public event Action<IDialogResult> RequestClose;

        public string ClosingTips
        {
            get => _ClosingTips; set {
                _ClosingTips = value;
                RaisePropertyChanged();
            }
        }

        string IDialogAware.Title { get;  }

        public ICommand ConfirmClosing { get; set; }

        public DeviceDialogViewModel()
        {
            ConfirmClosing = new DelegateCommand<object>(ConfirmClosingHandler);
        }

        

        bool IDialogAware.CanCloseDialog()
        {
            return true;
        }

        void IDialogAware.OnDialogClosed()
        {
            
        }

        void IDialogAware.OnDialogOpened(IDialogParameters parameters)
        {
            if(parameters.TryGetValue("deviceName", out string deviceName)){ 
                ClosingTips = $"您确认要关闭{deviceName}吗？";
            }
        }

        void ConfirmClosingHandler(object obj) {

            DialogParameters dialogParameters = new DialogParameters();

            // 关闭弹窗的同时向调用者传递参数
            dialogParameters.Add("time", DateTime.Now);

            RequestClose?.Invoke(new DialogResult(ButtonResult.OK,dialogParameters));
        }
    }
}
